<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%>
<script type="text/javascript">

		var prod_opera; //action url
	
    	//业务类别数据源
		var type_store = new Ext.data.JsonStore({
    		root: "data",
    		totalProperty: "total",
    		fields:["prodTypeId", "prodTypeName","prodTypeSate"],
			url : "product!queryProdsType.action"
    	});
    	
    	//业务类别下拉列表store
		var state_store= new Ext.data.JsonStore( {
			fields : ["id", "text"],
			data:[{id:"0",text:"禁用"},{id:"1",text:"启用"}]
		}); 
		
		//定义数据显示面板
		var p1 = {
			id: "p1",
			xtype: "grid",
			stripeRows:true,
			loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
			autoExpandColumn:"name",//自动填充
			autoExpandMin : 100,//自动填充最小宽度
			store: type_store,
			headersDisabled: true,
			sm : new Ext.grid.RowSelectionModel({//行选择监听,判断状态是否可审核
			listeners : {
				"rowselect" : function(s, index) {
					var state = type_store.getAt(index).data.prodTypeSate;
					if (state === "0") {//启用不能修改,不能审核
						Ext.getCmp("del_type").setDisabled(true);
					} else {
						Ext.getCmp("del_type").setDisabled(false);
					}
				}
			}
		}),
			columns:[
	        	new Ext.grid.RowNumberer({header: "序号", width: 35}),
    					{header: "业务类别编号",dataIndex: "prodTypeId" ,menuDisabled: true},
    					{header: "业务类别名称",dataIndex: "prodTypeName",	id:"name", menuDisabled: true},
    					{header: "状态",dataIndex: "prodTypeSate",hidden:true,menuDisabled: true,
							renderer:function(input){
					             if(""==input)return input;
		            			 var res2=state_store.getById(input);
								 return res2.data.text;
		            		}
		            	}
			],
			buttonAlign: "center",
			bbar: new Ext.ux.PagingToolbar({
            	pageSize: limit,
            	store:type_store
        	}),
			buttons:[
						{text : "新增",
							handler : function() {
								prod_opera = "product!addProdType.action";
								Ext.getCmp("p2").resetParam();
								Ext.getCmp("prodtype_panel").layout.setActiveItem(1);
								}
							},
							{
								text : "修改",
								handler : function() {
									prod_opera = "product!editProdType.action";
									var record = Ext.getCmp("p1").getSelectionModel().getSelected();
									if (record) {
										Ext.getCmp("p2").setParam(record.data);
										Ext.getCmp("prodtype_panel").layout.setActiveItem(1);
									} else {
										//Ext.Msg.alert("错误","没有选中任何行数据，无法进行此操作!");
											Ext.Msg.show({
		                                            title:"警告",
		                                            msg: "没有选中任何行数据，无法进行此操作!",
		                                            buttons: Ext.Msg.OK,
		                                            animEl: "elId",
		                                            icon:Ext.MessageBox.WARNING
		                                            });
									}
								}
							},
							{
								text : "删除",
								id:"del_type",
								handler : function() {
									var record = Ext.getCmp("p1").getSelectionModel().getSelected();
									if (record) {
										Ext.Ajax.request({
										clientValidation: true,
										params : {"prodinty.prodTypeId":record.data.prodTypeId},
										url: "product!prodCount.action",
										success: function(response, opts){
											var d=eval("("+response.responseText+")");
											if(d.code!="0"){
												Ext.Msg.show({
						                            title:"提示",
						                            msg: "该业务类别下有启用产品,不能删除!",
						                            buttons: Ext.Msg.OK,
						                            animEl: "elId",
						                            icon:Ext.MessageBox.ERROR
						                   	 	});
												return;
											}else{					
												Ext.Msg.confirm("警告","确定要删除吗?", function(btn){
												    if (btn == "yes"){
												    	Ext.Ajax.request( {
												    		url : "product!deleteProdType.action",
												    		params : {"prodinty.prodTypeId":record.data.prodTypeId,"prodinty.prodTypeName":record.data.prodTypeName},
												    		success: function(response, opts){
																var d=eval("("+response.responseText+")");
																if(d.data){
																	type_store.load({params:{start : 0,limit : limit}});
																	Ext.getCmp("prodtype_panel").layout.setActiveItem(0);
																	//Ext.Msg.alert("成功","操作成功!");
																		Ext.Msg.show({
						                                                           title:"提示",
						                                                           msg:"操作成功!",
						                                                           buttons:Ext.Msg.OK,
						                                                           icon: Ext.MessageBox.INFO
						                                                           });
																} else {
																	//Ext.Msg.alert("失败","操作失败!");
						                                                         Ext.Msg.show({
						                                                           title:"提示",
						                                                           msg:"操作失败!",
						                                                           buttons:Ext.Msg.OK,
						                                                           icon: Ext.MessageBox.ERROR
						                                                           });
																}
															}
												    	});
												    }
												});												
											}
										}
									});			
																	
							}
							 else {
								//Ext.Msg.alert("错误","没有任何行被选中，无法进行删除操作!");
									Ext.Msg.show({
		                                      title:"警告",
		                                      msg: "没有任何行被选中，无法进行删除操作!",
		                                      buttons: Ext.Msg.OK,
		                                      animEl: "elId",
		                                      icon:Ext.MessageBox.WARNING
		                                      });
							}
						}
					} ]
		};


		
		var p2 = {
			id: "p2",
		    labelWidth: 80,
			xtype: "form",
		    bodyStyle:"padding:5px 5px 0",
			defaultType: "textfield",
			items: [{
				id: "type_id", 
				fieldLabel: "业务类别编号",	
				name: "id",
				minLength:3,
				maxLength:3,
				vtype:"alphanum",
				allowBlank: false,
				anchor: "100%",
				listeners:{
				"change":function(e,newValue,oldValue){
					Ext.Ajax.request({
						clientValidation: true,
						params : {"prodinty.prodTypeId":newValue},
						url: "product!idYON.action",
						success: function(response, opts){
							var d=eval("("+response.responseText+")");
							if(d.data&&d.data.prodTypeSate=="1"){
								Ext.Msg.show({
		                            title:"警告",
		                            msg: "业务类别编号重复!",
		                            buttons: Ext.Msg.OK,
		                            animEl: "elId",
		                            icon:Ext.MessageBox.WARNING
		                   		});
								e.setValue(oldValue);
							} else {								
								return true;
							}
						}
					});
				}
				}
			},{
				id: "type_name", 
				vtype:"alphanumchinese",
					validator:function (val){
		        		if(val=="")return "该输入项为必输项！";
		        		if(Ext.fucCheckCHLength(val)>50){
		        			return "请输入50个字符范围内的值!<br>注：一个中文字符算两个字符的长度！";
		        		}
		        		return true;
		        	},
				fieldLabel: "业务类别名称",		
				allowBlank: false,
				name: "text",
				anchor: "100%"
			}],
			buttonAlign: "center",
			buttons:[
						{
							text : "保存",
							id:"save",
							handler : function(e) {
							
							var tyid = Ext.getCmp("type_id").getValue();
							var tyname = Ext.getCmp("type_name").getValue();
							if(tyid==""){
								//Ext.Msg.alert("提示","业务类别编号不能为空!");
								Ext.Msg.show({
		                               title:"警告",
		                               msg: "业务类别编号不能为空!",
		                               buttons: Ext.Msg.OK,
		                               animEl: "elId",
		                               icon:Ext.MessageBox.WARNING
		                               });
		                        return;
							}
							if(tyname==""){
								//Ext.Msg.alert("提示","业务类别名称不能为空!");	
								Ext.Msg.show({
		                               title:"警告",
		                               msg: "业务类别名称不能为空!",
		                               buttons: Ext.Msg.OK,
		                               animEl: "elId",
		                               icon:Ext.MessageBox.WARNING
		                              });
		                          return;						
							}
							if(!Ext.getCmp("p2").getForm().isValid()){
								//Ext.Msg.alert("提示","验证不通过,请验证是否有特殊字符!");
								Ext.Msg.show({
		                            title:"警告",
		                            msg: "验证不通过!",
		                            buttons: Ext.Msg.OK,
		                            animEl: "elId",
		                            icon:Ext.MessageBox.WARNING
		                            });
								return;
							}
							e.setDisabled(true);
							Ext.Ajax.request({
								clientValidation: true,
								params : {"prodinty.prodTypeId":tyid,
										"prodinty.prodTypeName":tyname},
								url: prod_opera,
								success: function(response, opts){
									var d=eval("("+response.responseText+")");
									if(d.data){
										type_store.load({params:{start : 0,limit : limit}});
										Ext.getCmp("prodtype_panel").layout.setActiveItem(0);
										//Ext.Msg.alert("成功","操作成功!");
										Ext.Msg.show({
		                                    title:"提示",
		                                    msg:"操作成功!",
		                                    buttons:Ext.Msg.OK,
		                                    icon: Ext.MessageBox.INFO
		                                });
		                                
									} else {
										//Ext.Msg.alert("失败","操作失败!");
										Ext.Msg.show({
		                                    title:"提示",
		                                    msg:"操作失败!",
		                                    buttons:Ext.Msg.OK,
		                                    icon: Ext.MessageBox.ERROR
		                                    });
									}
									Ext.getCmp("save").setDisabled(false);
								}
							});
							}
						},
						{
							text : "取消",
							handler : function() {
								this.ownerCt.ownerCt.ownerCt.layout.setActiveItem(0);
							}
						} ],
			setParam: function(data){
				Ext.getCmp("type_id").setValue(data.prodTypeId);
				Ext.getCmp("type_id").setDisabled(true);
				Ext.getCmp("type_name").setValue(data.prodTypeName);
			},
			resetParam: function(){
				Ext.getCmp("type_id").setValue("");
				Ext.getCmp("type_id").setDisabled(false);
				Ext.getCmp("type_name").setValue("");
			}
		};

		var protype_pel = new Ext.Panel({
			id: "prodtype_panel",
			border : false,		
			layout: "card",
			activeItem: 0,
			items: [p1, p2]
		});	
		type_store.load({params:{start : 0,limit : limit}});
		
	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(protype_pel);
	CenterPanel.doLayout();		
	</script>
